from microbit import *

class Servo(object):
    def __init__(self, servo_pin = pin1):
        self.pin = servo_pin
        self.pin.set_analog_period(20)
        self.write_angle(0)

    def write_angle(self, servo_angle):
        self.angle = servo_angle
        pulse_width = 0
        angle_output = 0
        pulse_width = (600 + (2400-600) * (self.angle-0) / (180-0))
        angle_output = (0 + (1023-0) * (pulse_width-0) / (20000-0))
        self.pin.write_analog(angle_output)
    
    def read_angle(self):
        return self.angle
